using System.Collections;
using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace AsterroidX
{
    public class PowerUpUnlockSystem : AbstractSystem
    {
        public List<string> UnlockedPowerUps;

        public HashSet<string> PowerUpsToUnlock;

        public readonly List<string> AllFirstStagePowerUps  = new List<string>()
            {
                PowerType.PowerUpGhost.ToString(),
                PowerType.PowerUpSpread.ToString(),
                PowerType.PowerUpCircle.ToString(),
                PowerType.PowerUpDuration.ToString(),
                PowerType.PowerUpRepetScreen.ToString(),
                PowerType.PowerUpBigBullet.ToString(),
                PowerType.PowerUpDamageUp.ToString(),
                PowerType.PowerUpRotatePlayer.ToString(),
                PowerType.PowerUpTornado.ToString()

            };

        private readonly List<string> mSecondStagePowerUps = new List<string>()
        {
            PowerType.PowerUpFullScreen.ToString(),
            PowerType. PowerUpRasengan.ToString(),
            PowerType.PowerUpCobra.ToString()
        };

        public  BindableProperty<bool> SecondStageUnlocked = new BindableProperty<bool>(false);
        protected override void OnInit()
        {
            UnlockedPowerUps=new List<string>();
           
            PowerUpsToUnlock = new HashSet<string>();
        }

        public void Reset()
        {
            UnlockedPowerUps.Clear();
            PowerUpsToUnlock.Clear();

            SecondStageUnlocked.Value = false;

            foreach(var powerUp in AllFirstStagePowerUps)
            {
                PowerUpsToUnlock.Add(powerUp);
            }

            var copy = PowerUpsToUnlock.ToList();

            var powerUp1=copy.GetAndRemoveRandomItem();

            var powerUp2 =copy.GetAndRemoveRandomItem();

            UnlockPowerUp(powerUp1);
            UnlockPowerUp(powerUp2);
        }

        public void UnlockPowerUp(string powerUp)
        {
            PowerUpsToUnlock.Remove(powerUp);
            UnlockedPowerUps.Add(powerUp);

            if(!SecondStageUnlocked.Value)
            {
                SecondStageUnlocked.Value = 
                    UnlockedPowerUps.Count== AllFirstStagePowerUps.Count;

                foreach(var item in mSecondStagePowerUps)
                {
                    UnlockedPowerUps.Add(item);
                }
            }
        }
    }
}
